﻿Imports System.Windows.Resources
Imports Windows.Storage
Imports System.Threading.Tasks
Imports Windows.System
Imports Microsoft.Phone.Storage
Imports System.Runtime.CompilerServices
Imports System.IO.IsolatedStorage
Imports System.Runtime.Serialization

Namespace IO
    Public Module FileHelper
        Public ReadOnly MyStore As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication
        Public Async Function ResToByte(info As StreamResourceInfo) As Task(Of Byte())
            If info Is Nothing Then Return Nothing
            Dim s = info.Stream
            Dim data(s.Length - 1) As Byte
            Await s.ReadAsync(data, 0, s.Length)
            Return data
        End Function

        Public Async Function GetLocalFiles() As Task(Of IReadOnlyList(Of StorageFile))
            Dim localFolder = ApplicationData.Current.LocalFolder
            Return Await localFolder.GetFilesAsync
        End Function

        Public Async Function GetLocalFiles(extname As String) As Task(Of IReadOnlyList(Of StorageFile))
            Dim files = Await GetLocalFiles()
            Return files.Where(Function(f) f.Name.EndsWith("." & extname))
        End Function

        Public Async Sub OpenFile(file As StorageFile)
            If file Is Nothing Then Return
            Await Launcher.LaunchFileAsync(file)
        End Sub

        Public Async Function GetSDCardFiles() As Task(Of IReadOnlyList(Of StorageFile))
            Dim externalStorageDevices = Await ExternalStorage.GetExternalStorageDevicesAsync
            Dim sdCard = externalStorageDevices.FirstOrDefault
            If sdCard Is Nothing Then
                MessageBox.Show("并没有发现SD卡，请检查SD卡是否插好。")
                Return Nothing
            End If
            Return Await sdCard.RootFolder.GetFilesAsync
        End Function

        Public Async Function GetSDCardFiles(extname As String) As Task(Of IReadOnlyList(Of StorageFile))
            Dim files = Await GetSDCardFiles()
            Return files.Where(Function(f) f.Name.EndsWith("." & extname))
        End Function

#Region "Serialization"
        <Extension>
        Public Sub NormalWrite(Of T)(value As T, fileName As String)
            Using isolatedStorageFileStream As New IsolatedStorageFileStream(fileName, System.IO.FileMode.OpenOrCreate, MyStore)
                Dim s As New DataContractSerializer(GetType(T))
                s.WriteObject(isolatedStorageFileStream, value)
            End Using
        End Sub

        Public Function NormalRead(Of T)(fileName As String) As T
            Using isolatedStorageFileStream As New IsolatedStorageFileStream(fileName, System.IO.FileMode.Open, MyStore)
                Dim s As New DataContractSerializer(GetType(T))

                Dim value = s.ReadObject(isolatedStorageFileStream)
                isolatedStorageFileStream.Close()
                Return CType(value, T)
            End Using
        End Function
#End Region
    End Module
End Namespace